package com.jd.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.jd.domain.User;
import com.jd.feign.UserFeignClient;
import com.jd.sentinel.OrderFallbackClass;
import com.jd.sentinel.blockHandlerClassOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private UserFeignClient userFeignClient;

    @GetMapping("/getOrderById/{id}")
    @SentinelResource(value = "order-getOrderById",
            blockHandler = "blockHandlerOrder",
            blockHandlerClass = blockHandlerClassOrder.class,
            fallback = "getOrderByIdFallback",
            fallbackClass = OrderFallbackClass.class) // 开启限流
    public User getOrderById(@PathVariable("id") Long id){
//        if (true){
//            throw new RuntimeException("熔断了！！！");
//        }
        int i = 1/0;
        // 调用OpenFeign接口，通信User服务
        User userById = userFeignClient.getUserById(id);
        System.out.println(userById);
        return new User(3L,"傻逼哇","我看看你呢");
      // return userFeignClient.getUserById(id);

    }


//    // 必须增加一个参数Throwable（熔断）本类
//    public User getOrderByIdFallback(@PathVariable("id") Long id,Throwable e){
//        return new User(-1L, "服务降级触发！", "服务不可调用，遭不住了");
//    }



//    //服务降级方法本类
//    public User blockHandlerOrder(@PathVariable("id") Long id, BlockException e){
//        return new User(-1L, "服务限流触发！", "当前服务流量过大，请稍后重试！遭不住了");
//    }


}
